package bb.lanxing.network;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.baidu.mapapi.model.LatLng;
import com.umeng.socialize.net.utils.SocializeProtocolConstants;

import android.os.Build;

import bb.lanxing.lib.kt.model.geo.IGeoPoint;
import bb.lanxing.model.json.BiciLatlng;
import bb.lanxing.util.BiCiCoorConverter;
import bb.lanxing.util.Log;
import gov.nist.core.Separators;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class BaiduHttpClient {
    public static String AK = "cre7wgUnAp6LbbovQmBkChjBu2zDt3Cp";
    public static String DIRECTION_API = "http://api.map.baidu.com/direction/v1";
    public static final String TAG = "HttpClient";
    public static final OkHttpClient client = new OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS).build();

    public static String buildUrlByParams(String url, HashMap<String, Object> params) {
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        if (params != null) {
            Iterator<String> it2 = params.keySet().iterator();
            if (it2.hasNext()) {
                sb.append(Separators.QUESTION);
            }
            while (it2.hasNext()) {
                try {
                    String next = it2.next();
                    Object obj = params.get(next);
                    sb.append(next);
                    sb.append(Separators.EQUALS);
                    if ("mcode".equals(next)) {
                        sb.append(obj.toString());
                    } else {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                            sb.append(URLEncoder.encode(obj.toString(), StandardCharsets.UTF_8));
                        } else {
                            sb.append(obj.toString());
                        }
                    }
                    sb.append("&");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    public static void buildDirection(List<BiciLatlng> points, Callback callback) {
        BiciLatlng biciLatlng = points.get(0);
        BiciLatlng biciLatlng2 = points.get(points.size() - 1);
        LatLng earth2Baidu = BiCiCoorConverter.earth2Baidu(new LatLng(biciLatlng.getLatitude(), biciLatlng.getLongitude()));
        LatLng earth2Baidu2 = BiCiCoorConverter.earth2Baidu(new LatLng(biciLatlng2.getLatitude(), biciLatlng2.getLongitude()));
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("mode", "riding");
        hashMap.put("origin_region", "");
        hashMap.put("destination_region", "");
        hashMap.put("output", "json");
        hashMap.put(SocializeProtocolConstants.PROTOCOL_KEY_AK, AK);
        hashMap.put("origin", earth2Baidu.latitude + "," + earth2Baidu.longitude);
        hashMap.put("destination", earth2Baidu2.latitude + "," + earth2Baidu2.longitude);
        Request build = new Request.Builder().addHeader("Accept", "application/json").url(buildUrlByParams(DIRECTION_API, hashMap)).build();
        Log.d("HttpClient", build.toString());
        System.out.println("buildDirection11: " + build);
        client.newCall(build).enqueue(callback);
    }

    public static Call buildDirectionByGeoPoints(List<IGeoPoint> points) {
        IGeoPoint baidu = points.get(0).toBaidu(true);
        IGeoPoint baidu2 = points.get(points.size() - 1).toBaidu(true);
        LatLng latLng = new LatLng(baidu.getLatitude(), baidu.getLongitude());
        LatLng latLng2 = new LatLng(baidu2.getLatitude(), baidu2.getLongitude());
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("mode", "riding");
        hashMap.put("origin_region", "");
        hashMap.put("destination_region", "");
        hashMap.put("output", "json");
        hashMap.put(SocializeProtocolConstants.PROTOCOL_KEY_AK, AK);
        hashMap.put("origin", latLng.latitude + "," + latLng.longitude);
        hashMap.put("destination", latLng2.latitude + "," + latLng2.longitude);
        Request build = new Request.Builder().addHeader("Accept", "application/json").url(buildUrlByParams(DIRECTION_API, hashMap)).build();
        Log.d("HttpClient", build.toString());
        return client.newCall(build);
    }
}